<template>
  <el-row :gutter="20">
    <el-col :span="5">
      <category @tree-node-click="treenodeclick"></category>
    </el-col>
    <el-col :span="19">
      <div class="mod-config">
        <el-form :inline="true" :model="filter" @keyup.enter.native="fetch()">
          <el-form-item label="组名">
            <el-input v-model="filter.attrGroupName" placeholder="请填写组名" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button @click="fetch()">查询</el-button>
            <el-button type="success" @click="getAllDataList()">查询全部</el-button>
            <el-button
              type="primary"
              @click="addOrUpdateHandle()"
            >
              新增
            </el-button>
            <el-button
              type="danger"
              @click="deleteHandle()"
              :disabled="dataListSelections.length <= 0"
            >
              批量删除
            </el-button>
          </el-form-item>
        </el-form>
        <el-table
          :data="dataSource"
          border
          v-loading="dataListLoading"
          @selection-change="selectionChangeHandle"
          style="width: 100%;"
        >
          <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>
          <el-table-column prop="icon" header-align="center" align="center" label="组图标"></el-table-column>
          <el-table-column prop="attrGroupName" header-align="center" align="center" label="组名"></el-table-column>
          <el-table-column prop="descript" header-align="center" align="center" label="描述"></el-table-column>
          <el-table-column
            fixed="right"
            header-align="center"
            align="center"
            width="150"
            label="操作"
          >
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="relationHandle(scope.row.attrGroupId)">关联</el-button>
              <el-button
                type="text"
                size="small"
                @click="addOrUpdateHandle(scope.row.attrGroupId)"
              >修改</el-button>
              <el-button type="text" size="small" @click="deleteHandle(scope.row.attrGroupId)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          background
          layout="prev, pager, next"
          :page-size="pagination.size"
          :current-page="pagination.current"
          :total="pagination.total"
          @current-change="fetch"
        />
        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="fetch"></add-or-update>

        <!-- 修改关联关系 -->
        <relation-update v-if="relationVisible" ref="relationUpdate" @refreshData="fetch"></relation-update>
      </div>
    </el-col>
  </el-row>
</template>

<script>
import Category from "../common/category";
import AddOrUpdate from "./components/attrgroup-add-or-update";
import RelationUpdate from "./components/attr-group-relation";
import { queryForPage, deleteBatchAttrGroup } from '@/api/product/attrgroup'

export default {
  components: { Category, AddOrUpdate, RelationUpdate },
  props: {},
  data() {
    return {
      filter: {
        key: "",
        catelogId: null
      },
      pagination: {
        current: 1,
        size: 5,
        total: 0
      },
      dataSource: [],
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false,
      relationVisible: false
    }
  },
  activated() {
    this.fetch();
  },
  methods: {
    // 处理分组与属性的关联
    relationHandle (groupId) {
      this.relationVisible = true;
      this.$nextTick(() => {
        this.$refs.relationUpdate.init(groupId);
      });
    },
    // 感知树节点被点击
    treenodeclick (data, node, component) {
      if (node.level === 3) {
        this.filter.catelogId = data.catId
        this.fetch()
        this.filter.catelogId = null
      }
    },
    // 查询所有
    getAllDataList () {
      this.filter = {
        key: "",
        catelogId: null
      }
      this.pagination = {
        current: 1,
          size: 10,
          total: 0
      }
      this.fetch()
    },
    // 获取数据列表
    async fetch (e) {
      if (e) {
        this.pagination.current = e
      }
      const params = { ...this.filter, ...this.pagination }
      const { data } = await queryForPage(params)
      const { records, current, size, total } = data
      this.dataSource = records
      this.pagination = { current, size, total }
    },
    // 多选
    selectionChangeHandle (val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle (id) {
      this.addOrUpdateVisible = true;
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id);
      });
    },
    // 删除
    deleteHandle (id) {
      let ids = []
      if (id) {
        ids.push(id)
      } else {
        ids = this.dataListSelections.map( obj => { return obj.attrGroupId })
        this.dataListSelections = []
      }
      this.$confirm('此操作将删除该商品类型, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        try {
          await deleteBatchAttrGroup(ids)
          if (this.dataSource.length === ids.length) {
            this.pagination.current--
          }
          await this.fetch()
        } catch (error) {
          console.log(error)
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消取消操作'
        })
      })
    }
  }
};
</script>
<style scoped>
</style>
